/*
 * MathUtil.java
 *
 * Created on 20 de marzo de 2008, 8:06
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package pe.com.nextel.util.math;
/**
 *
 * @author edgard
 */
public class MathUtil {
    
    private final static int PRECISION = 10;
    public static char SIMBOLO_DECIMAL = '.';
    
    public static boolean isNumber(String b){
        
        if(b == null || "".equals(b)) return true;
        
        b = b.replace(',', '.');
        
        if(".".equals(b)) return false;
    
        try{
            Double.parseDouble(b);
            return true;
        }catch(Exception e){ 
            System.out.println("b="+b);
            e.printStackTrace();
            return false;}
    }
    
    public static String sumar(String a, String b) {
        if(!(b != null && !"".equals(b))) return a;
        if(!(a != null && !"".equals(a))) return b;
        
        b = b.replace(',', '.');
        a = a.replace(',', '.');
        
        com.mot.iden.math.Float fa = new com.mot.iden.math.Float(a);
        com.mot.iden.math.Float fb = new com.mot.iden.math.Float(b);
        
        fa.setPrecision(PRECISION);
        fb.setPrecision(PRECISION);
        
        return com.mot.iden.math.Math.add(fa, fb).toString();
    }
    
    public static String restar(String a, String b) {
        if(!(b != null && !"".equals(b))) return a;
        if(!(a != null && !"".equals(a))) return b;
         
        b = b.replace(',', '.');
        a = a.replace(',', '.');
        
        com.mot.iden.math.Float fa = new com.mot.iden.math.Float(a);
        com.mot.iden.math.Float fb = new com.mot.iden.math.Float(b);
        
        fa.setPrecision(PRECISION);
        fb.setPrecision(PRECISION);
        
        return com.mot.iden.math.Math.subtract(fa, fb).toString();
    }
    
    public static String multiplicar(String a, String b) {
        if(!(b != null && !"".equals(b))) b = "0";
        if(!(a != null && !"".equals(a))) a = "0";
        
        b = b.replace(',', '.');
        a = a.replace(',', '.');
        
        com.mot.iden.math.Float fa = new com.mot.iden.math.Float(a);
        com.mot.iden.math.Float fb = new com.mot.iden.math.Float(b);
        
        fa.setPrecision(PRECISION);
        fb.setPrecision(PRECISION);
        
        return com.mot.iden.math.Math.multiply(fa, fb).toString();
    }
    
    public static String dividir(String a, String b) {
        if(!(b != null && !"".equals(b))) return "0";
        if(!(a != null && !"".equals(a))) a = "0";
        
        b = b.replace(',', '.');
        a = a.replace(',', '.');
        
        com.mot.iden.math.Float fa = new com.mot.iden.math.Float(a);
        
        if(Float.parseFloat(b) == 0){
            return "0";
        }
        
        com.mot.iden.math.Float fb = new com.mot.iden.math.Float(b);
        
        fa.setPrecision(PRECISION);
        fb.setPrecision(PRECISION);
        
//        System.out.println("fa.getPrecision()=" + fa.getPrecision());
//        System.out.println("fb.getPrecision()=" + fb.getPrecision());
        
        return com.mot.iden.math.Math.divide(fa, fb).toString();
    }
    
    public static String abs(String a) {
        if(!(a != null && !"".equals(a))) return null;
    
        a = a.replace(',', '.');
        
        com.mot.iden.math.Float fa = new com.mot.iden.math.Float(a);
        
        fa.setPrecision(PRECISION);
        
        return com.mot.iden.math.Math.abs(fa).toString();
    }
    
    /**
     * @Deprecate usar el de Number.setScale
     */
    public static String setScale(String a, int i) {
        if(!(a != null && !"".equals(a))) return null;
        
        a = a.replace(',', '.');
        
        com.mot.iden.math.Float fa = new com.mot.iden.math.Float(a);
        
        fa.setPrecision(i);
        
        return fa.toString();
    }
    
    public static boolean esMayor(String a, String b) {
        if(!(b != null && !"".equals(b))) return true;
        if(!(a != null && !"".equals(a))) return false;        
        
         b = b.replace(',', '.');
        a = a.replace(',', '.');
        
        return (Double.parseDouble(a) > Double.parseDouble(b));
        
    }
    
    public static boolean esMayorIgual(String a, String b) {
        
        if(!(b != null && !"".equals(b))) return true;
        if(!(a != null && !"".equals(a))) return false;
        
         b = b.replace(',', '.');
        a = a.replace(',', '.');
        
        return (Double.parseDouble(a) >= Double.parseDouble(b));
        
    }
    
    public static final int FLOAT_EQUALS = 0, FLOAT_MINOR = -1, FLOAT_MAJOR = 1;
    
    public static int compareFloat(String value1, String value2) {
        
        if(!(value1 != null && !"".equals(value1))) value1 ="0";
        if(!(value2 != null && !"".equals(value2))) value2 ="0";
        
         value1 = value1.replace(',', '.');
        value2 = value2.replace(',', '.');
        
        float v1 = java.lang.Float.parseFloat(value1);
        float v2 = java.lang.Float.parseFloat(value2);
        
        if (v1 == v2) {
            return FLOAT_EQUALS;
            
        } else if (v1 > v2) {
            return FLOAT_MAJOR;
            
        } else {
            return FLOAT_MINOR;
        }
        
    }
    
    
    public static int compareFloat(com.mot.iden.math.Float value1,
            com.mot.iden.math.Float value2) {
        return compareFloat(value1.toString(), value2.toString());
    }
    
    public static String porcentaje(String porcentaje, String entero){
        
        
        
        return multiplicar(dividir(porcentaje, entero), "100");
    }
    
}
